package com.fjwt.gz.task.stat;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjwt.gz.db.entity.AgentAppEntity;
import com.fjwt.gz.db.entity.AgentInfoEntity;
import com.fjwt.gz.db.entity.OldFansInfoEntity;
import com.fjwt.gz.db.entity.StatMemberInfoEntity;
import com.fjwt.gz.db.entity.StatMemberNumEntity;
import com.fjwt.gz.db.vo.AgentInfoVO;
import com.fjwt.gz.service.impl.AgentAppService;
import com.fjwt.gz.service.impl.AgentInfoService;
import com.fjwt.gz.service.impl.OldFansInfoService;
import com.fjwt.gz.service.impl.StatMemberInfoService;
import com.fjwt.gz.service.mapper.StatMemberNumMapper;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 机构会员通报表统计定时任务
 */
@Component
@Slf4j
public class StatMemberNumTask {

    @Autowired
    private StatMemberInfoService statMemberInfoService;
    @Autowired
    private AgentInfoService agentInfoService;
    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    @Autowired
    private AgentAppService agentAppService;
    @Autowired
    private OldFansInfoService oldFansInfoService;

    private static final int QUERY_PAGE_SIZE = 10000; //每次查询数量

    private static final int SAVE_BATCH_SIZE = 2000; //每次插入数量

//    @Scheduled(cron = "${cron.StatMemberNumTask}")
    @Transactional(rollbackFor = Exception.class)
    public void start() {
        DateTime date = DateUtil.date();
        log.info("开始进入：机构会员通报表统计定时任务，开始时间:{}", date);
        DateTime beginOfDay = DateUtil.beginOfDay(date);
        // 日
        DateTime statDate = DateUtil.offsetDay(beginOfDay, -1);
        // 上周开始时间
        DateTime beginOfLastWeek = DateUtil.beginOfWeek(DateUtil.offsetWeek(date, -1));
        // 上周结束时间
        DateTime endOfLastWeek = DateUtil.beginOfWeek(date);
        // 本月开始时间
        DateTime beginOfMonth = DateUtil.beginOfMonth(date);
        // 本年开始时间
        DateTime beginOfYear = DateUtil.beginOfYear(date);

        int currentPageIndex = 1; // 当前页数

        // 查询有小程序的机构
        List<AgentAppEntity> agentAppList = agentAppService.list(AgentAppEntity.gw().eq(AgentAppEntity::getType, 1));
        if (ObjectUtil.isEmpty(agentAppList)) {
            return;
        }
        Map<String, String> appIdAgentNoMap = agentAppList.stream().collect(Collectors.toMap(AgentAppEntity::getAppId, AgentAppEntity::getAgentNo));
        // 查询机构信息
        List<AgentInfoEntity> agentInfoList = agentInfoService.list();
        Map<String, AgentInfoEntity> agentMap = agentInfoList.stream().collect(Collectors.toMap(AgentInfoEntity::getAgentNo, Function.identity()));

        // 封装所需数据对象
        Map<String, StatMemberNumEntity> map = new HashMap<>();
        appIdAgentNoMap.forEach((k,v) -> {
            map.put(k, new StatMemberNumEntity().setLiteAppId(k).setStatDate(statDate));

            Set<String> agentNo4s = new HashSet<>();
            getAgentNo4(agentInfoService.getAgentInfos(v), agentNo4s);
            agentNo4s.forEach(item -> {
                StatMemberNumEntity statMemberNumEntity = new StatMemberNumEntity().setLiteAppId(k).setStatDate(statDate).setAgentNo(item).setAgentNo4(item);
                AgentInfoEntity agentInfoEntity = agentMap.get(item);
                statMemberNumEntity.setNextAgentNum(agentInfoEntity.getNextAgentNum());
                statMemberNumEntity.setAgentName4(agentInfoEntity.getAgentName());
                statMemberNumEntity.setAgentNo3(agentInfoEntity.getAgentNo3());
                statMemberNumEntity.setAgentName3(agentMap.get(agentInfoEntity.getAgentNo3()).getAgentName());
                statMemberNumEntity.setAgentNo2(agentInfoEntity.getAgentNo2());
                statMemberNumEntity.setAgentName2(agentMap.get(agentInfoEntity.getAgentNo2()).getAgentName());
                statMemberNumEntity.setAgentNo1(agentInfoEntity.getAgentNo1());
                statMemberNumEntity.setAgentName1(agentMap.get(agentInfoEntity.getAgentNo1()).getAgentName());
                map.put(k + item, statMemberNumEntity);
            });
        });

        // 查询老用户数量
        long count = oldFansInfoService.count();
        List<OldFansInfoEntity> oldFansList = oldFansInfoService.list(OldFansInfoEntity.gw()
                .isNotNull(OldFansInfoEntity::getFansId)
                .select(OldFansInfoEntity::getFansId, OldFansInfoEntity::getSyncActivityId));
        Map<Long, OldFansInfoEntity> oldFansInfoMap = oldFansList.stream().collect(Collectors.toMap(OldFansInfoEntity::getFansId, Function.identity()));

        // 统计每个机构的数据
        while (true) {
            try {
                IPage<StatMemberInfoEntity> statMemberInfoPage = statMemberInfoService.page(new Page(currentPageIndex, QUERY_PAGE_SIZE), StatMemberInfoEntity.gw().lt(StatMemberInfoEntity::getCreatedAt, beginOfDay));
                log.info("查询页数：{}，size:{}", currentPageIndex, QUERY_PAGE_SIZE);

                // 本次查询结果为空 跳出当前循环
                if (ObjectUtil.isEmpty(statMemberInfoPage.getRecords())) {
                    break;
                }
                for (StatMemberInfoEntity record : statMemberInfoPage.getRecords()) {
                    StatMemberNumEntity statMemberNumEntity;
                    if (ObjectUtil.isEmpty(record.getAgentNo4())) {
                        statMemberNumEntity = map.get(record.getLiteAppId());
                    }else {
                        statMemberNumEntity = map.get(record.getLiteAppId() + record.getAgentNo4());
                    }

                    // 总
                    statMemberNumEntity.setTotalMemberNum(ObjectUtil.isEmpty(statMemberNumEntity.getTotalMemberNum()) ? 1 : statMemberNumEntity.getTotalMemberNum() + 1);
                    if (ObjectUtil.isNotEmpty(record.getCstmNo())) {
                        statMemberNumEntity.setTotalCstmMemberNum(ObjectUtil.isEmpty(statMemberNumEntity.getTotalCstmMemberNum()) ? 1 : statMemberNumEntity.getTotalCstmMemberNum() + 1);
                    }
                    statMemberNumEntity.setTotalOldMemberNum((int) count);
                    if (ObjectUtil.isNotEmpty(record.getSyncTime()) && oldFansInfoMap.containsKey(record.getFansId())) {
                        statMemberNumEntity.setTotalSyncMemberNum(ObjectUtil.isEmpty(statMemberNumEntity.getTotalSyncMemberNum()) ? 1 : statMemberNumEntity.getTotalSyncMemberNum() + 1);
                        if (ObjectUtil.isNotEmpty(oldFansInfoMap.get(record.getFansId()).getSyncActivityId())) {
                            statMemberNumEntity.setActivitySyncMemberNum(ObjectUtil.isEmpty(statMemberNumEntity.getActivitySyncMemberNum()) ? 1 : statMemberNumEntity.getActivitySyncMemberNum() + 1);
                        }
                    }

                    // 日
                    if (record.getCreatedAt().compareTo(statDate) >= 0) {
                        statMemberNumEntity.setDayMemberNum(ObjectUtil.isEmpty(statMemberNumEntity.getDayMemberNum()) ? 1 : statMemberNumEntity.getDayMemberNum() + 1);
                        if (ObjectUtil.isNotEmpty(record.getCstmNo())) {
                            statMemberNumEntity.setDayCstmMemberNum(ObjectUtil.isEmpty(statMemberNumEntity.getDayCstmMemberNum()) ? 1 : statMemberNumEntity.getDayCstmMemberNum() + 1);
                        }
                        if (ObjectUtil.isNotEmpty(record.getSyncTime()) && oldFansInfoMap.containsKey(record.getFansId())) {
                            statMemberNumEntity.setDaySyncMemberNum(ObjectUtil.isEmpty(statMemberNumEntity.getDaySyncMemberNum()) ? 1 : statMemberNumEntity.getDaySyncMemberNum() + 1);
                        }
                    }

                    // 周
                    if (record.getCreatedAt().compareTo(beginOfLastWeek) >= 0 && record.getCreatedAt().compareTo(endOfLastWeek) < 0) {
                        statMemberNumEntity.setLastWeekMemberNum(ObjectUtil.isEmpty(statMemberNumEntity.getLastWeekMemberNum()) ? 1 : statMemberNumEntity.getLastWeekMemberNum() + 1);
                        if (ObjectUtil.isNotEmpty(record.getCstmNo())) {
                            statMemberNumEntity.setLastWeekCstmMemberNum(ObjectUtil.isEmpty(statMemberNumEntity.getLastWeekCstmMemberNum()) ? 1 : statMemberNumEntity.getLastWeekCstmMemberNum() + 1);
                        }
                        if (ObjectUtil.isNotEmpty(record.getSyncTime()) && oldFansInfoMap.containsKey(record.getFansId())) {
                            statMemberNumEntity.setLastWeekSyncMemberNum(ObjectUtil.isEmpty(statMemberNumEntity.getLastWeekSyncMemberNum()) ? 1 : statMemberNumEntity.getLastWeekSyncMemberNum() + 1);
                        }
                    }

                    // 月
                    if (record.getCreatedAt().compareTo(beginOfMonth) >= 0) {
                        statMemberNumEntity.setMonthMemberNum(ObjectUtil.isEmpty(statMemberNumEntity.getMonthMemberNum()) ? 1 : statMemberNumEntity.getMonthMemberNum() + 1);
                        if (ObjectUtil.isNotEmpty(record.getCstmNo())) {
                            statMemberNumEntity.setMonthCstmMemberNum(ObjectUtil.isEmpty(statMemberNumEntity.getMonthCstmMemberNum()) ? 1 : statMemberNumEntity.getMonthCstmMemberNum() + 1);
                        }
                        if (ObjectUtil.isNotEmpty(record.getSyncTime()) && oldFansInfoMap.containsKey(record.getFansId())) {
                            statMemberNumEntity.setMonthSyncMemberNum(ObjectUtil.isEmpty(statMemberNumEntity.getMonthSyncMemberNum()) ? 1 : statMemberNumEntity.getMonthSyncMemberNum() + 1);
                        }
                    }

                    // 年
                    if (record.getCreatedAt().compareTo(beginOfYear) >= 0) {
                        statMemberNumEntity.setYearMemberNum(ObjectUtil.isEmpty(statMemberNumEntity.getYearMemberNum()) ? 1 : statMemberNumEntity.getYearMemberNum() + 1);
                        if (ObjectUtil.isNotEmpty(record.getCstmNo())) {
                            statMemberNumEntity.setYearCstmMemberNum(ObjectUtil.isEmpty(statMemberNumEntity.getYearCstmMemberNum()) ? 1 : statMemberNumEntity.getYearCstmMemberNum() + 1);
                        }
                        if (ObjectUtil.isNotEmpty(record.getSyncTime()) && oldFansInfoMap.containsKey(record.getFansId())) {
                            statMemberNumEntity.setYearSyncMemberNum(ObjectUtil.isEmpty(statMemberNumEntity.getYearSyncMemberNum()) ? 1 : statMemberNumEntity.getYearSyncMemberNum() + 1);
                        }
                    }
                }

                currentPageIndex++;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        List<StatMemberNumEntity> agentNo4EntityList = map.values().stream().toList();

        // 三级
        Map<String, List<StatMemberNumEntity>> agentNo3Values = agentNo4EntityList.stream()
                .filter(item -> ObjectUtil.isNotEmpty(item.getAgentNo3()))
                .collect(Collectors.groupingBy(item -> item.getAgentNo3() + item.getLiteAppId()));

        // 计算四级排名
        agentNo3Values.forEach((k, v) -> assignRanksOptimized(v));

        // 累加3级信息
        List<StatMemberNumEntity> agentNo3EntityList = accumulationStatMemberNum(agentNo3Values, 3, statDate, agentMap);

        // 二级
        Map<String, List<StatMemberNumEntity>> agentNo2Values = agentNo3EntityList.stream()
                .filter(item -> ObjectUtil.isNotEmpty(item.getAgentNo2()))
                .collect(Collectors.groupingBy(item -> item.getAgentNo2() + item.getLiteAppId()));

        // 计算三级排名
        agentNo2Values.forEach((k, v) -> assignRanksOptimized(v));

        // 累加2级信息
        List<StatMemberNumEntity> agentNo2EntityList = accumulationStatMemberNum(agentNo2Values, 2, statDate, agentMap);

        // 一级
        Map<String, List<StatMemberNumEntity>> agentNo1Values = agentNo2EntityList.stream()
                .filter(item -> ObjectUtil.isNotEmpty(item.getAgentNo1()))
                .collect(Collectors.groupingBy(item -> item.getAgentNo1() + item.getLiteAppId()));

        // 计算二级排名
        agentNo1Values.forEach((k, v) -> assignRanksOptimized(v));

        // 累加1级信息
        List<StatMemberNumEntity> agentNo1EntityList = accumulationStatMemberNum(agentNo1Values, 1, statDate, agentMap);
        // 计算一级排名
        assignRanksOptimized(agentNo1EntityList);

        // 合并新增
        addBatch(Stream.of(agentNo4EntityList, agentNo3EntityList, agentNo2EntityList, agentNo1EntityList)
                .flatMap(List::stream)
                .collect(Collectors.toList()));
        log.info("结束：机构会员通报表统计定时任务，结束时间:{}", DateUtil.now());
    }

    /**
     * 累加不同等级机构下的数据
     * @param agentGroupMap 机构等级分组数据
     * @param level 机构等级
     * @param statDate 日期
     * @param agentMap 机构信息
     * @return
     */
    private static List<StatMemberNumEntity> accumulationStatMemberNum(Map<String, List<StatMemberNumEntity>> agentGroupMap, Integer level, DateTime statDate, Map<String, AgentInfoEntity> agentMap) {
        List<StatMemberNumEntity> agentStatMemberNumEntityList = new ArrayList<>();
        agentGroupMap.forEach((k, v) -> {
            StatMemberNumEntity basicsStat = v.get(0);
            String liteAppId = basicsStat.getLiteAppId();
            StatMemberNumEntity statMemberNumEntity = new StatMemberNumEntity();
            statMemberNumEntity.setLiteAppId(liteAppId);
            statMemberNumEntity.setStatDate(statDate);
            statMemberNumEntity.setTotalOldMemberNum(basicsStat.getTotalOldMemberNum());
            switch (level) {
                case 4:
                    statMemberNumEntity.setAgentNo(basicsStat.getAgentNo4());
                    statMemberNumEntity.setNextAgentNum(agentMap.get(basicsStat.getAgentNo4()).getNextAgentNum());
                    statMemberNumEntity.setAgentNo4(basicsStat.getAgentNo4());
                    statMemberNumEntity.setAgentName4(basicsStat.getAgentName4());
                    statMemberNumEntity.setAgentNo3(basicsStat.getAgentNo3());
                    statMemberNumEntity.setAgentName3(basicsStat.getAgentName3());
                    statMemberNumEntity.setAgentNo2(basicsStat.getAgentNo2());
                    statMemberNumEntity.setAgentName2(basicsStat.getAgentName2());
                    statMemberNumEntity.setAgentNo1(basicsStat.getAgentNo1());
                    statMemberNumEntity.setAgentName1(basicsStat.getAgentName1());
                    break;
                case 3:
                    statMemberNumEntity.setAgentNo(basicsStat.getAgentNo3());
                    statMemberNumEntity.setNextAgentNum(agentMap.get(basicsStat.getAgentNo3()).getNextAgentNum());
                    statMemberNumEntity.setAgentNo3(basicsStat.getAgentNo3());
                    statMemberNumEntity.setAgentName3(basicsStat.getAgentName3());
                    statMemberNumEntity.setAgentNo2(basicsStat.getAgentNo2());
                    statMemberNumEntity.setAgentName2(basicsStat.getAgentName2());
                    statMemberNumEntity.setAgentNo1(basicsStat.getAgentNo1());
                    statMemberNumEntity.setAgentName1(basicsStat.getAgentName1());
                    break;
                case 2:
                    statMemberNumEntity.setAgentNo(basicsStat.getAgentNo2());
                    statMemberNumEntity.setNextAgentNum(agentMap.get(basicsStat.getAgentNo2()).getNextAgentNum());
                    statMemberNumEntity.setAgentNo2(basicsStat.getAgentNo2());
                    statMemberNumEntity.setAgentName2(basicsStat.getAgentName2());
                    statMemberNumEntity.setAgentNo1(basicsStat.getAgentNo1());
                    statMemberNumEntity.setAgentName1(basicsStat.getAgentName1());
                    break;
                case 1:
                    statMemberNumEntity.setAgentNo(basicsStat.getAgentNo1());
                    statMemberNumEntity.setNextAgentNum(agentMap.get(basicsStat.getAgentNo1()).getNextAgentNum());
                    statMemberNumEntity.setAgentNo1(basicsStat.getAgentNo1());
                    statMemberNumEntity.setAgentName1(basicsStat.getAgentName1());
                    break;
                default:
                    break;
            }

            for (StatMemberNumEntity statMemberNum : v) {
                // 总
                statMemberNumEntity.setTotalMemberNum(safeAdd(statMemberNumEntity.getTotalMemberNum() ,statMemberNum.getTotalMemberNum()));
                statMemberNumEntity.setTotalCstmMemberNum(safeAdd(statMemberNumEntity.getTotalCstmMemberNum() ,statMemberNum.getTotalCstmMemberNum ()));
                statMemberNumEntity.setTotalSyncMemberNum(safeAdd(statMemberNumEntity.getTotalSyncMemberNum() ,statMemberNum.getTotalSyncMemberNum ()));
                statMemberNumEntity.setActivitySyncMemberNum(safeAdd(statMemberNumEntity.getActivitySyncMemberNum() ,statMemberNum.getActivitySyncMemberNum ()));

                // 日
                statMemberNumEntity.setDayMemberNum(safeAdd(statMemberNumEntity.getDayMemberNum() ,statMemberNum.getDayMemberNum ()));
                statMemberNumEntity.setDayCstmMemberNum(safeAdd(statMemberNumEntity.getDayCstmMemberNum() ,statMemberNum.getDayCstmMemberNum ()));
                statMemberNumEntity.setDaySyncMemberNum(safeAdd(statMemberNumEntity.getDaySyncMemberNum() ,statMemberNum.getDaySyncMemberNum ()));

                // 周
                statMemberNumEntity.setLastWeekMemberNum(safeAdd(statMemberNumEntity.getLastWeekMemberNum() ,statMemberNum.getLastWeekMemberNum ()));
                statMemberNumEntity.setLastWeekCstmMemberNum(safeAdd(statMemberNumEntity.getLastWeekCstmMemberNum() ,statMemberNum.getLastWeekCstmMemberNum ()));
                statMemberNumEntity.setLastWeekSyncMemberNum(safeAdd(statMemberNumEntity.getLastWeekSyncMemberNum() ,statMemberNum.getLastWeekSyncMemberNum ()));

                // 月
                statMemberNumEntity.setMonthMemberNum(safeAdd(statMemberNumEntity.getMonthMemberNum() ,statMemberNum.getMonthMemberNum ()));
                statMemberNumEntity.setMonthCstmMemberNum(safeAdd(statMemberNumEntity.getMonthCstmMemberNum() ,statMemberNum.getMonthCstmMemberNum ()));
                statMemberNumEntity.setMonthSyncMemberNum(safeAdd(statMemberNumEntity.getMonthSyncMemberNum() ,statMemberNum.getMonthSyncMemberNum ()));

                // 年
                statMemberNumEntity.setYearMemberNum(safeAdd(statMemberNumEntity.getYearMemberNum() ,statMemberNum.getYearMemberNum ()));
                statMemberNumEntity.setYearCstmMemberNum(safeAdd(statMemberNumEntity.getYearCstmMemberNum() ,statMemberNum.getYearCstmMemberNum ()));
                statMemberNumEntity.setYearSyncMemberNum(safeAdd(statMemberNumEntity.getYearSyncMemberNum() ,statMemberNum.getYearSyncMemberNum ()));
            }

            agentStatMemberNumEntityList.add(statMemberNumEntity);
        });
        return agentStatMemberNumEntityList;
    }

    // 累加方法
    private static Integer safeAdd(Integer a, Integer b) {
        int valueA = a != null ? a : 0;
        int valueB = b != null ? b : 0;
        return valueA + valueB;
    }

    /**
     * 批量新增
     * @param record
     */
    private void addBatch(List<StatMemberNumEntity> record){
        // 使用原生 批处理的形式进行插入
        try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
            // 分批插入数据
            List<List<StatMemberNumEntity>> partition =
                    Lists.partition(record, SAVE_BATCH_SIZE);

            StatMemberNumMapper statMemberNumMapper = sqlSession.getMapper(StatMemberNumMapper.class);
            partition.parallelStream()
                    .forEach(statMemberNumMapper::insert);
            sqlSession.commit();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    // 处理排名规则
    public void assignRanksOptimized(List<StatMemberNumEntity> list) {
        // 预先过滤并缓存有代理号的列表
        List<StatMemberNumEntity> filteredList = list.stream().filter(item -> ObjectUtil.isNotEmpty(item.getAgentNo())).toList();
        // 维度1排名
        assignRank(filteredList, Comparator.comparing(StatMemberNumEntity::getTotalSyncMemberNum, Comparator.nullsFirst(Comparator.naturalOrder())).reversed(), StatMemberNumEntity::setTotalSyncMemberNumSort);

        // 维度2排名
        assignRank(filteredList, Comparator.comparing(StatMemberNumEntity::getDayCstmMemberNum, Comparator.nullsFirst(Comparator.naturalOrder())).reversed(), StatMemberNumEntity::setDayCstmMemberSort);

        // 维度3排名
        assignRank(filteredList, Comparator.comparing(StatMemberNumEntity::getLastWeekCstmMemberNum, Comparator.nullsFirst(Comparator.naturalOrder())).reversed(), StatMemberNumEntity::setLastWeekCstmMemberSort);

        // 维度4排名
        assignRank(filteredList, Comparator.comparing(StatMemberNumEntity::getMonthCstmMemberNum, Comparator.nullsFirst(Comparator.naturalOrder())).reversed(), StatMemberNumEntity::setMonthCstmMemberSort);

        // 维度5排名
        assignRank(filteredList, Comparator.comparing(StatMemberNumEntity::getYearCstmMemberNum, Comparator.nullsFirst(Comparator.naturalOrder())).reversed(), StatMemberNumEntity::setYearCstmMemberSort);
    }

    // 计算排名
    private void assignRank(List<StatMemberNumEntity> list,
                            Comparator<StatMemberNumEntity> comparator,
                            BiConsumer<StatMemberNumEntity, Integer> rankSetter) {
        List<StatMemberNumEntity> sortedList = list.parallelStream().sorted(comparator).toList();

        int currentRank = 1;
        for (int i = 0; i < sortedList.size(); i++) {
            // 如果不是第一个元素且与前一个元素不相等，更新排名
            if (i > 0 && comparator.compare(sortedList.get(i), sortedList.get(i - 1)) != 0) {
                currentRank = i + 1;
            }
            rankSetter.accept(sortedList.get(i), currentRank);
        }
    }

    // 递归获取所有四级机构号
    private void getAgentNo4(AgentInfoVO agentInfoVO, Set<String> agentNo4Set) {
        if (ObjectUtil.isNotEmpty(agentInfoVO.getChildren())) {
            for (AgentInfoVO child : agentInfoVO.getChildren()) {
                getAgentNo4(child, agentNo4Set);
            }
        }else {
            agentNo4Set.add(agentInfoVO.getAgentNo());
        }
    }
}
